package com.xhj.bigfactory.bigFactory.GC_JVM;

import java.lang.ref.SoftReference;
/**
 * -
 *
 * @author 见见 pure_xhj@163.com @Date 2021-08-06 @Time 0:03
 */
public class SoftReferenceDemo {
  public static void main(String[] args) {
    Object o1 = new Object();
    SoftReference<Object> softReference = new SoftReference<>(o1);
    System.out.println("o1 = " + o1);
    System.out.println("softReference.get() = " + softReference.get());
    Object o2 = softReference.get();
    System.out.println("o2 = " + o2);

    System.out.println("=======================");
    o1 = null;
    System.gc();
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println("o1 = " + o1);
    System.out.println("softReference.get() = " + softReference.get());
    Object o22 = softReference.get();
    System.out.println("o22 = " + o22);

    System.out.println("=======================");
    byte[] bytes = new byte[30 * 1024 * 1024];
    System.out.println("o22 = " + o22);
    System.out.println("softReference.get() = " + softReference.get());
  }
}
